package lumingweihua.future.cn.lumingweihua.ui.mine;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.zhusx.core.adapter.Lib_BaseRecyclerAdapter;
import com.zhusx.core.interfaces.IHttpResult;
import com.zhusx.core.network.HttpRequest;
import com.zhusx.core.utils._Densitys;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import lumingweihua.future.cn.lumingweihua.R;
import lumingweihua.future.cn.lumingweihua.home.cheyuan.HuoyuanDetailActivity;
import lumingweihua.future.cn.lumingweihua.network.LoadData;
import lumingweihua.future.cn.lumingweihua.network.LoadingHelper;
import lumingweihua.future.cn.lumingweihua.release.ReleaseGoodsActivity;
import lumingweihua.future.cn.lumingweihua.ui.BaseAppFragment;
import lumingweihua.future.cn.lumingweihua.ui.BaseCompatActivity;
import lumingweihua.future.cn.lumingweihua.ui.mine.domain.GoodsSourceData;
import lumingweihua.future.cn.lumingweihua.widget.TitleBarView;

/**
 * todo 我发布的货源
 * Author       zhusx
 * Created      2018/3/19 17:19
 */

public class MineGoodsSourceActivity extends BaseCompatActivity {
    @BindView(R.id.tabLayout)
    TabLayout tabLayout;
    @BindView(R.id.viewPager)
    ViewPager viewPager;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mine_goods_source);
        ButterKnife.bind(this);
        initView();

        TitleBarView title_bar= findViewById(R.id.title_bar);
        title_bar.rightTV.setTextColor(Color.parseColor("#2683f5"));
    }

    public void initView() {
        viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            String[] names = new String[]{"发布中", "待审核", "已拒绝", "已结束"};
            GoodsSourceFragment[] fragments = new GoodsSourceFragment[names.length];

            @Override
            public Fragment getItem(int position) {
                if (fragments[position] == null) {
                    fragments[position] = new GoodsSourceFragment();
                    switch (position) {
                        case 0:
                            fragments[position].setData(2);
                            break;
                        case 1:
                            fragments[position].setData(1);
                            break;
                        case 2:
                            fragments[position].setData(3);
                            break;
                        case 3:
                            fragments[position].setData(4);
                            break;
                    }
                }
                return fragments[position];
            }

            @Override
            public int getCount() {
                return 4;
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return names[position];
            }
        });
        viewPager.setOffscreenPageLimit(4);
        tabLayout.setupWithViewPager(viewPager);
    }

    @OnClick({R.id.tv_titleBar_right})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_titleBar_right:
                startActivity(new Intent(v.getContext(), ReleaseGoodsActivity.class));
                break;
        }
    }

    public static class GoodsSourceFragment extends BaseAppFragment {
        @BindView(R.id.recyclerView)
        RecyclerView recyclerView;

        Lib_BaseRecyclerAdapter<GoodsSourceData> adapter;
        LoadData<List<GoodsSourceData>> loadData;
        int status;

        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            return inflater.inflate(R.layout.fragment_mine_carsource, container, false);
        }

        @Override
        public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);
            ButterKnife.bind(this, view);
            initView();
        }

        private void initView() {
            loadData = new LoadData<>(LoadData.Api.个人中心我发布的货源, this);
            recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
                @Override
                public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                    outRect.set(0, _Densitys.dip2px(view.getContext(), 10), 0, 0);
                }
            });
            recyclerView.setAdapter(adapter = new Lib_BaseRecyclerAdapter<GoodsSourceData>() {
                @Override
                protected void __bindViewHolder(_ViewHolder viewHolder, int i, final GoodsSourceData s) {
                    viewHolder.setText(R.id.tv_type, s.type);
                    viewHolder.setText(R.id.tv_fromCity, s.loading);
                    viewHolder.setText(R.id.tv_toCity, s.unload);
                    viewHolder.setText(R.id.tv_weight, "共" + s.weight + "吨  剩" + s.surplus_weight + "吨");
                    viewHolder.setText(R.id.tv_date, s.use_time);
                    viewHolder.setText(R.id.tv_price, "报价: " + s.good_price );
                    viewHolder.rootView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            startActivity(new Intent(view.getContext(), HuoyuanDetailActivity.class).putExtra("id", s.id));
                        }
                    });
                }

                @Override
                protected int __getLayoutResource(int i) {
                    return R.layout.item_list_mine_goods_source;
                }

                @Override
                public void onBindViewHolder(_ViewHolder holder, int position) {
                    super.onBindViewHolder(holder, position);
                    if (position == getItemCount() - 1 && getItemCount() % LoadData.PAGE_ITME == 0) {
                        loadData._reLoadData(false);
                    }
                }
            });
            loadData._setOnLoadingListener(new LoadingHelper<List<GoodsSourceData>>(recyclerView, loadData) {
                @Override
                public void __onComplete(HttpRequest<Object> request, IHttpResult<List<GoodsSourceData>> data) {
                    if (request.isRefresh) {
                        adapter._setItemToUpdate(data.getData());
                    } else {
                        adapter._addItemToUpdate(data.getData());
                    }
                }
            });
            loadData._refreshData(status);
        }


        public void setData(int status) {
            this.status = status;
        }
    }
}
